/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import com.sun.xml.internal.ws.api.ha.StickyFeature;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import logica.Funcionario;
import persistencia.BancoDeDados;
import persistencia.FuncionarioPesistencia;

/**
 *
 * @author XP
 */
public class CadastroFuncionarioAdm extends javax.swing.JFrame {
    
    ResultSet resultset;
    FuncionarioPesistencia funcionarioBD;
    

    /**
     * Creates new form CadastroTicket
     */
    public CadastroFuncionarioAdm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lbNome = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        textMatricula = new javax.swing.JTextField();
        lbCadastro = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        comoboxFilial = new javax.swing.JComboBox();
        btCadastrar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jRadioTI = new javax.swing.JRadioButton();
        jRadioFiscal = new javax.swing.JRadioButton();
        jRadioDiretoria = new javax.swing.JRadioButton();
        jRadioLogistica = new javax.swing.JRadioButton();
        jRadioTransporte = new javax.swing.JRadioButton();
        jRadioRH = new javax.swing.JRadioButton();
        jRadioADM = new javax.swing.JRadioButton();
        jRadioVendas = new javax.swing.JRadioButton();
        jRadioCompras = new javax.swing.JRadioButton();
        jRadioFinanceio = new javax.swing.JRadioButton();
        btEsquerda = new javax.swing.JButton();
        btDireita = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbNome.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbNome.setText("Nome:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Matrícula:");

        lbCadastro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbCadastro.setText("Cadastro de Funcionário ADM");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Empresa:");

        comoboxFilial.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A4", "A6", "G1", "T1", "B1", "B2" }));

        btCadastrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/BTSave.png"))); // NOI18N
        btCadastrar.setText("Cadastrar");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        btSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/btsair.png"))); // NOI18N
        btSair.setText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Setor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N

        buttonGroup1.add(jRadioTI);
        jRadioTI.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioTI.setText("TI");

        buttonGroup1.add(jRadioFiscal);
        jRadioFiscal.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioFiscal.setText("Fiscal");

        buttonGroup1.add(jRadioDiretoria);
        jRadioDiretoria.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioDiretoria.setText("Diretoria");

        buttonGroup1.add(jRadioLogistica);
        jRadioLogistica.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioLogistica.setText("Logística");

        buttonGroup1.add(jRadioTransporte);
        jRadioTransporte.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioTransporte.setText("Transporte");

        buttonGroup1.add(jRadioRH);
        jRadioRH.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioRH.setText("RH");

        buttonGroup1.add(jRadioADM);
        jRadioADM.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioADM.setText("Administrativo");

        buttonGroup1.add(jRadioVendas);
        jRadioVendas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioVendas.setText("Vendas");

        buttonGroup1.add(jRadioCompras);
        jRadioCompras.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioCompras.setText("Compras");

        buttonGroup1.add(jRadioFinanceio);
        jRadioFinanceio.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioFinanceio.setText("Financeiro");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioLogistica)
                    .addComponent(jRadioDiretoria)
                    .addComponent(jRadioFiscal)
                    .addComponent(jRadioTI)
                    .addComponent(jRadioTransporte, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(53, 53, 53)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioRH)
                    .addComponent(jRadioADM)
                    .addComponent(jRadioVendas)
                    .addComponent(jRadioFinanceio)
                    .addComponent(jRadioCompras))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioRH)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioADM)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioVendas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioCompras)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioFinanceio))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioTI)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioFiscal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioDiretoria)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioLogistica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioTransporte)))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        btEsquerda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Esquerda.png"))); // NOI18N
        btEsquerda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEsquerdaActionPerformed(evt);
            }
        });

        btDireita.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Direita.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lbNome)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textNome))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(comoboxFilial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lbCadastro)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btEsquerda, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btDireita, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCadastrar)
                        .addGap(10, 10, 10)
                        .addComponent(btSair)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(lbCadastro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbNome)
                        .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(comoboxFilial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(31, 31, 31)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btDireita, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(btEsquerda, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(btCadastrar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
         System.exit(0);
    }//GEN-LAST:event_btSairActionPerformed

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        BancoDeDados.conecta();
        String nome = textNome.getText();
        int matricula = Integer.parseInt(textMatricula.getText());
        
        String setor = "NULL";
        
        if(jRadioTI.isSelected()){
            setor = "TI";
        }else 
            if(jRadioFiscal.isSelected()){
                setor = "FISCAL";
            }else
                if(jRadioDiretoria.isSelected()){
                    setor = "DIRETORIA";
                }else 
                    if(jRadioLogistica.isSelected()){
                        setor = "LOGISTICA";
                    }else
                        if(jRadioTransporte.isSelected()){
                            setor = "TRANSPORTE";
                        }else
                            if(jRadioRH.isSelected()){
                                setor = "RH";
                            }else
                                if(jRadioADM.isSelected()){
                                    setor = "ADM";
                                }else
                                    if(jRadioVendas.isSelected()){
                                        setor = "VENDAS";
                                    }else
                                        if(jRadioCompras.isSelected()){
                                            setor = "COMPRAS";
                                        }else
                                            if(jRadioFinanceio.isSelected()){
                                                setor = "FINANCEIRO";
                                            }
                                            
        String filial = (String) comoboxFilial.getSelectedItem();
        Funcionario funcionario = new Funcionario(nome, matricula, setor, filial);
        funcionarioBD.salvarUsuario(funcionario);
        JOptionPane.showMessageDialog(null, "Funcionario Cadastrado com Sucesso!");
        BancoDeDados.desconectar();
    }//GEN-LAST:event_btCadastrarActionPerformed

    private void btEsquerdaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEsquerdaActionPerformed
        try {
            resultset.previous();
            btCadastrar.setEnabled(false);
            btSair.setEnabled(true);
            btDireita.setEnabled(true);            
            refreshFuncionario();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(CadastroFuncionarioAdm.this, "Não existe mais registros",
                    ex.getMessage(), JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btEsquerdaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroFuncionarioAdm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroFuncionarioAdm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroFuncionarioAdm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroFuncionarioAdm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CadastroFuncionarioAdm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCadastrar;
    private javax.swing.JButton btDireita;
    private javax.swing.JButton btEsquerda;
    private javax.swing.JButton btSair;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox comoboxFilial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioADM;
    private javax.swing.JRadioButton jRadioCompras;
    private javax.swing.JRadioButton jRadioDiretoria;
    private javax.swing.JRadioButton jRadioFinanceio;
    private javax.swing.JRadioButton jRadioFiscal;
    private javax.swing.JRadioButton jRadioLogistica;
    private javax.swing.JRadioButton jRadioRH;
    private javax.swing.JRadioButton jRadioTI;
    private javax.swing.JRadioButton jRadioTransporte;
    private javax.swing.JRadioButton jRadioVendas;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbCadastro;
    private javax.swing.JLabel lbNome;
    private javax.swing.JTextField textMatricula;
    private javax.swing.JTextField textNome;
    // End of variables declaration//GEN-END:variables

    private void refreshFuncionario() {
        try {
            comoboxFilial.setSelectedItem(resultset.getString("filial"));
            textMatricula.setText(resultset.getString("matricula"));            
            textNome.setText(resultset.getString("nome"));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(CadastroFuncionarioAdm.this, "Não existe mais registros",
                         ex.getMessage(), JOptionPane.INFORMATION_MESSAGE);
        }    
    }
}
